package com.animee.day11.slice;

import com.animee.day11.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

/**分析逻辑
 * 1.点击清空，将2个TextFiled设置为空，性别设置为男
 * 2.点击测算，获取输入内容，计算，将我们信息传入到下一面
 * */
public class BMIAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    TextField sgTf,tzTf;
    RadioContainer sexRc;
    Button calBtn,clearBtn;
    RadioButton mRb,fRb;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abilitiy_bmi);
        initView();
    }
    private void initView() {
        mRb = findComponentById(ResourceTable.Id_bmi_rb_m);
        fRb = findComponentById(ResourceTable.Id_bmi_rb_f);
        sgTf = findComponentById(ResourceTable.Id_bmi_tf_sg);
        tzTf = findComponentById(ResourceTable.Id_bmi_tf_tz);
        sexRc = findComponentById(ResourceTable.Id_bmi_rc);
        calBtn = findComponentById(ResourceTable.Id_bmi_btn_cal);
        clearBtn = findComponentById(ResourceTable.Id_bmi_btn_clear);
        calBtn.setClickedListener(this);
        clearBtn.setClickedListener(this);
    }
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bmi_btn_cal:
                calHealtyIndex();
                break;
            case ResourceTable.Id_bmi_btn_clear:
                sgTf.setText("");
                tzTf.setText("");
                mRb.setChecked(true);
                break;
        }
    }

    //计算健康指数
    private void calHealtyIndex() {
        String sgStr = sgTf.getText();
        String tzStr = tzTf.getText();
        double sg = 0,tz = 0;
        try {
            sg = Double.valueOf(sgStr)/100;
            tz = Double.valueOf(tzStr);
        }catch (Exception e){
            showToast("输入内容有误，重新输入！");
            return;
        }
        double bmi = tz/(sg*sg);
        //获取选的性别
        String sex = "女";
        if (mRb.isChecked()) {
            sex = "男";
        }
       // 携带数据，进入下一个页面,
        Intent intent = new Intent();
        intent.setParam("sg",sg);
        intent.setParam("tz",tz);
        intent.setParam("bmi",bmi);
        intent.setParam("sex",sex);
        present(new BMIInfoAbilitySlice(),intent);
    }

    private void showToast(String s) {
        new ToastDialog(this)
                .setText(s)
                .show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
